package cn.itcast.sms.action;

import cn.itcast.bos.domain.constants.Constants;
import cn.itcast.bos.domain.take_delivery.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.sms.utils.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.ws.rs.core.MediaType;
import java.io.File;
import java.io.FileOutputStream;

import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

@Controller
@Namespace("/")
@ParentPackage("json-default")
@Scope("protoType")
@SuppressWarnings("all")
public class PromotionAction extends BaseAction<Promotion> {

    //到bos_management系统中查找数据的方法
    @Action(value = "promotion_pageQuery",results = {@Result(name = "success",type = "json")})
    public String pageQuery(){
        PageBean<Promotion> pageBean = WebClient.create(Constants.BOS_MANAGEMENT_URL+"/bos_management/service/promotionService/pageQuery?page=" + page + "&rows=" + rows).
                accept(MediaType.APPLICATION_JSON).get(PageBean.class);

        ActionContext.getContext().getValueStack().push(pageBean);

        return SUCCESS;
    }
    //用来实现freemarker的方法
    @Action(value = "promotion_showDetail")
    public String showDetail(){
        //先判断文件是否存在
        String htmlRealPath = ServletActionContext.getServletContext().getRealPath("/freemarker");
        File root = new File(htmlRealPath);
        if (!root.exists()) {
            root.mkdirs();
        }

        File htmlFile = new File(htmlRealPath + "/" +model.getId()+".html");
        try {
        if(!htmlFile.exists()){
            Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);

                configuration.setDirectoryForTemplateLoading(
                        new File(ServletActionContext.getServletContext().getRealPath("WEB-INF/freemarker")));

                //获取模板对象
                Template template= configuration.getTemplate("promotion_detail.ftl","utf-8");
                //文件不存在
                Promotion promotion = WebClient.create(Constants.BOS_MANAGEMENT_URL+"/bos_management/service/promotionService/showDetail?id="+model.getId()).
                        accept(MediaType.APPLICATION_JSON).get(Promotion.class);
                Map<String ,Object> parmameterMap = new HashMap<>();
                parmameterMap.put("promotion",promotion);
                //合并输出
                template.process(parmameterMap,new OutputStreamWriter(new FileOutputStream(htmlFile),"UTF-8"));
        }
            //如果存在,直接将文件写会
            ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
            FileUtils.copyFile(htmlFile,ServletActionContext.getResponse().getOutputStream());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return NONE;
    }

}
